package com.zhugang.week13.queue;

/**
 * @program algorithms
 * @description: CircleQueue
 * @author: chanzhugang
 * @create: 2022/11/01 11:32
 */
public class CircleQueue {

    private int[] items;
    private int n;
    private int head;
    private int tail;

    public CircleQueue(int[] items, int n) {
        this.items = items;
        this.n = n;
    }

    public boolean enqueue(int item) {
        if ((tail + 1) % n == head) {
            // 队满
            return false;
        }
        items[tail] = item;
        tail = (tail + 1) % n;
        return true;
    }

    public int dequeue() {
        if (head == tail) {
            // 队空
            return -1;
        }
        int item = items[head];
        head = (head + 1) % n;
        return item;
    }

}